<?php
/**
 * Created by PhpStorm.
 * Register: czm
 * Date: 2019-06-18
 * Time: 11:33
 */
namespace app\admin\controller;

use think\Controller;
use think\Db;

class Notice extends Controller
{
    public function noticesList()
    {
        $list = Db::name('notice')->order('id desc')->select();
        $this->assign('data',$list);
        return $this->fetch();
    }

    public function noticesAdd()
    {
        if (request()->isPost())
        {
            if ($_FILES['pic']['tmp_name'])
            {
                $file = request()->file('pic');//文件上传
                $info = $file->move(ROOT_PATH . 'public' . DS . 'static/images');//移动文件，DS代表/或者\斜杠
                $da = $info->getSaveName();//图片的保存路径
            }
            $data = [
                'name'  =>input('name'),
                'pic'   => $da,
                'state' =>  input('state'),
                'content'   =>  input('content'),
            ];
            $res = Db::name('notice')->insert($data);
            if ($res)
            {
                return show(200);
            }else{
                return show(403);
            }
        }
        return $this->fetch();
    }

    public function noticesEdit()
    {
        $id = input('id');
        $a = db('notice')->find($id);
        $this->assign('a', $a);
        if (request()->isPost())
        {
            if ($_FILES['pic']['tmp_name'])
            {
                $file = request()->file('pic');//文件上传
                $info = $file->move(ROOT_PATH . 'public' . DS . 'static/images');//移动文件，DS代表/或者\斜杠
                $da = $info->getSaveName();//图片的保存路径
            }
            $data = [
                'name'  =>input('name'),
                'pic'   => $da,
                'state' =>  input('state'),
                'content'   =>  input('content'),
            ];
            $res = Db::name('notice')->insert($data);
            if ($res)
            {
                return show(200);
            }else{
                return show(403);
            }
        }
        return $this->fetch();
    }

    //上架下架
    public function noticesActive()
    {
        // 0上架状态  1下架状态
        $id = $_POST['id'];
        $active = $_POST['state'];
//        var_dump($id);exit();
        if($active == 0){
            db('notice')->where('id',$id)->update(['state'=>0]);
            return show(200);
//            $this->success('上架成功');
        }else if ($active == 1){
            db('notice')->where('id',$id)->update(['state'=>1]);
            return show(403);
//            $this->error('下架成功');
        }
    }

    public function noticesDel()
    {
        $id = $_POST['id'];
        $data = Db::name('notice')->where('id',$id)->delete($id);
        if($data){
            return show(200);
        }else{
            return show(403);
        }
        return $this->fetch();
    }
}